/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cms.action.manage;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.entity.Admin;
import com.shishuo.cms.entity.Order;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.service.OrderService;
import com.shishuo.cms.service.UserService;

@Controller
@RequestMapping("/manage/user")
public class ManageUserAction extends ManageBaseAction {

	@Autowired
	private UserService userService;
	@Autowired
	private OrderService orderService;
	/**
	 * 
	 * 进入用户管理页面
	 */
	@RequestMapping(value = "/list.htm", method = RequestMethod.GET)
	public String manage(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap) {
		modelMap.put("pageVo", userService.getAllListPage(pageNum));
		return "manage/user/list";
	}
	
	/**
	 * 
	 * 进入订单管理页面
	 */
	@RequestMapping(value = "/order/list.htm", method = RequestMethod.GET)
	public String order(
			@RequestParam(value = "p", defaultValue = "1") int pageNum,
			ModelMap modelMap,@RequestParam(value = "userId", required=false)Integer userId) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("userId", userId);
		modelMap.put("pageVo", orderService.getAllList(map, pageNum));
		return "manage/order/list";
	}
	
	/**
	 * 进入单个admmin页面
	 * 
	 */
	@RequestMapping(value = "/order/update.htm", method = RequestMethod.GET)
	public String update(
			@RequestParam(value = "orderId", defaultValue = "0") int orderId,
			ModelMap modelMap, HttpServletRequest request) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("id", orderId);
		modelMap.put("order", orderService.getAllList(map, 1).getList().get(0));
		return "manage/order/update";
	}
	
	
	/**
	 * 发货
	 * 
	 */

	@ResponseBody
	@RequestMapping(value = "/order/send.json", method = RequestMethod.POST)
	public JsonVo<String> send(@RequestParam(value = "orderId") int orderId,
			HttpServletRequest request) {
		JsonVo<String> json = new JsonVo<String>();
		try {
			Order order=new Order();
			order.setId(orderId);
			order.setStatus(2);
			orderService.update(order);
			json.setResult(true);
		} catch (Exception e) {
			json.setResult(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}
	
	/**
	 * 删除订单
	 * 
	 */

	@ResponseBody
	@RequestMapping(value = "/order/update.json", method = RequestMethod.POST)
	public JsonVo<String> update(Order order,
			HttpServletRequest request) {
		JsonVo<String> json = new JsonVo<String>();
		try {
			orderService.update(order);
			json.setResult(true);
		} catch (Exception e) {
			json.setResult(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}

	/**
	 * 删除订单
	 * 
	 */

	@ResponseBody
	@RequestMapping(value = "/order/delete.json", method = RequestMethod.POST)
	public JsonVo<String> deleteOrder(@RequestParam(value = "orderId") int orderId,
			HttpServletRequest request) {
		JsonVo<String> json = new JsonVo<String>();
		try {
			orderService.deleteOrder(orderId);
			json.setResult(true);
		} catch (Exception e) {
			json.setResult(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}

	/**
	 * 删除用户
	 * 
	 */

	@ResponseBody
	@RequestMapping(value = "/delete.json", method = RequestMethod.POST)
	public JsonVo<String> delete(@RequestParam(value = "userId") long userId,
			HttpServletRequest request) {
		JsonVo<String> json = new JsonVo<String>();
		try {
			userService.deleteUser(userId);
			json.setResult(true);
		} catch (Exception e) {
			json.setResult(false);
			json.setMsg(e.getMessage());
		}
		return json;
	}
}
